<?php

class people extends Module {
	
	public function __construct () {
		parent::__construct (__CLASS__);
		
		$this->extend ('__custom_people');
	}
	
	public function __default ()
	{
		$typesController = TypesController::getInstance();
		$objectsController = ObjectsController::getInstance ();
		
		$oUserType = $typesController->getTypeByName ('user');
		
		$currentPage = getRequest ('page', 'int');
		if ($currentPage != 0) {
			$currentPage--;
		}
		
		if (!$onPage = Regedit::getInstance()->get ('//modules/people/rating_on_page')) {
			$onPage = 12;
		}
		
		$sel = new Selection;
		$sel->addType ($oUserType);
		$sel->setOrder ('rating', false);
		$sel->addFilter ('login', '', '!=');
		$sel->addLimit ($currentPage * $onPage, $onPage);
		
		$aObjects = $objectsController->getObjectsList($sel->getList ());
		
		$response = new ModuleResponse;
		$response->setTemplate ('people/index');
		
		foreach ($aObjects as $object) {
			$rating = $object->getValue ('rating');
			$object->setValue ('rating',  number_format ($rating, 2, ',', ' '));			
			$response->addObject ($object);
		}
		
		$response->addObject (new Array2XML ('pages', array (
			'onpage' => $onPage,
			'total' => $sel->getTotalCount (),
			'page' => ++$currentPage
		)));
		
		return $response;
	}
	
	public function search () {
		$typesController = TypesController::getInstance ();
		$objectsController = ObjectsController::getInstance ();
		
		$this->setPageTitle (lang ('search_title',__CLASS__));
		
		$currentPage = getRequest ('page', 'int');
		if ($currentPage > 0) {
			$currentPage--;
		}
		
		$onPage = 12; // TODO
		
		$oUserType = $typesController->getTypeByName ('user');
		
		$sel = new Selection;
		$sel->addType ($oUserType);
		$sel->addLimit ($currentPage * $onPage, $onPage);
		$sel->addFilter ('group', 3, '!=');
		$sel->setOrder ('rating', false);
		$this->detectFilters ($oUserType, $sel);
		
		$response = new ModuleResponse;
		$response->setTemplate ('people/search');
		$response->setAttribute ('search-all', '0');
		$response->setAttribute ('search-word', '');
		
		// Если посик по всем свойства
		if (getRequest ('word', 'bool')) {
			$searchWord = getRequest ('word');
			$this->searchByAll ($oUserType, $sel, $searchWord);
			$response->setAttribute ('search-all', '1');
			$response->setAttribute ('search-word', $searchWord);
		}
		
		$aObjects = $objectsController->getObjectsList($sel->getList ());
		
		foreach ($aObjects as $object) {
			$response->addObject ($object);
		}
		
		$response->addObject (new Array2XML ('pages', array (
			'onpage' => $onPage,
			'total' => $sel->getTotalCount (),
			'page' => ++$currentPage
		)));
		
		return $response;
	}
	
	public function getUsersCountries () {
		$aCountries = FDump::getCountObjectValues (array (TypesController::getInstance()->getTypeByName('user')), 'country');
		
		$k = 40 / (log ($aCountries['max'] - $aCountries['min'] + 1.1));
		
		$res = array ();
		
		for ($i = 0; $i < 15; ++$i) {
			if (!isset ($aCountries['values'][$i])) {
				break;
			}
			
			$size = log ($aCountries['values'][$i]['cnt'] + 1 - $aCountries['min']) * $k;
			$size = $size > 10 ? $size : 10;
			$size = $size < 40 ? $size : 40;
			
			$res[]['country'] = array (
				'value' => $aCountries['values'][$i]['value'],
				'cnt'   => $aCountries['values'][$i]['cnt'],
				'size'  => $size
			);
		}
		
		$response = new ModuleResponse;
		$response->addObject (new Array2XML ('countries', $res));
		
		return $response;
	}
	
	public function getUsersCities () {
		$aCities = FDump::getCountObjectValues (array (TypesController::getInstance()->getTypeByName('user')), 'city');
		
		$k = 40 / (log ($aCities['max'] - $aCities['min'] + 1.1));
		
		$res = array ();
		
		for ($i = 0; $i < 15; ++$i) {
			if (!isset ($aCities['values'][$i])) {
				break;
			}
			
			$size = log ($aCities['values'][$i]['cnt'] + 1 - $aCities['min']) * $k;
			$size = $size > 10 ? $size : 10;
			$size = $size < 40 ? $size : 40;
			
			$res[]['city'] = array (
				'value' => $aCities['values'][$i]['value'],
				'cnt'   => $aCities['values'][$i]['cnt'],
				'size'  => $size
			);
		}
		
		$response = new ModuleResponse;
		$response->addObject (new Array2XML ('cities', $res));
		
		return $response;	
	}
	
	public function getNewUsers () {
		$objectsController = ObjectsController::getInstance ();
		$oType = TypesController::getInstance()->getTypeByName('user');
		
		$sel = new Selection;
		$sel->addType ($oType);
		$sel->setOrder ('date', false);
		$sel->addFilter ('login', '', '!=');
		$sel->addLimit (12);
		
		$aObjects = $objectsController->getObjectsList($sel->getList ());
		
		$response = new ModuleResponse;
		
		foreach ($aObjects as $object) {
			$response->addObject ($object);
		}
		
		return $response;
	}
	
	public function stat () {
		$arr = FDump::getCountObjectValues (array (TypesController::getInstance()->getTypeByName('user')), 'gender');
		
		$cntMan = 0;
		$cntWoman = 0;
		$cntOther = 0;
		
		foreach ($arr['values'] as $gender) {
			switch ($gender['value']) {
				case 'man' : {
					$cntMan = $gender['cnt'];
					break;
				}
				case 'woman' : {
					$cntWoman = $gender['cnt'];
					break;
				}
				default : {
					$cntOther = $gender['cnt'];
				}
			}
		}
		
		$all = $cntMan + $cntWoman + $cntOther;
		
		$prMan = 100 / $all * $cntMan;
		$prWoman = 100 / $all * $cntWoman;
		$prOther = 100 / $all * $cntOther;
		
		$response = new ModuleResponse;
		$response->addObject (new Array2XML ('gender', array (
			'man' => round ($prMan) . '% (' . $cntMan . ')',
			'woman' => round ($prWoman) . '% (' . $cntWoman . ')',
			'other' => round ($prOther) . '% (' . $cntOther . ')'
		)));
		
		return $response;
	}
	
	private function searchByAll (Type $oType, Selection $selection, $string) {
		$string = (string) $string;
		
		$aFields = $oType->getFields ();
		$aFilters = array ();
		
		foreach ($aFields as $oField) {
			if (!$oField->isReal () || !$oField->isSearch ()) {
				continue; 
			}
			
			$aFilters[] = array ($oField->getName (), '%' . $string . '%', 'LIKE');
		}
		
		$selection->addFiltersGroup ($aFilters, 'OR');
	}
	
}